package sowing_record

import (
	rich_err "app/error"
	"app/models"
	"app/service"
	"app/service/orders"
	"app/utils"
	"errors"
	"math"
	"strconv"

	"github.com/jinzhu/gorm"

	"github.com/gin-gonic/gin"
)

func Gets(context *gin.Context) ([]*models.SowingRecordDetail, *models.Pager, *rich_err.RichError) {
	base := models.Orm.Model(&models.SowingRecord{}).
		Select("sowing_record.*, goadmin_users.name as manager_name,  orders.name as orders_name").
		Joins("LEFT JOIN goadmin_users ON sowing_record.manager_id = goadmin_users.id").
		Joins("LEFT JOIN orders ON sowing_record.order_id = orders.id")

	results, pager, err := service.GetDetails[models.SowingRecordDetail](context, base, models.SowingRecord{}.TableName(), map[string]string{"manager_name": "goadmin_users.name", "orders_name": "orders.name"})

	if err != nil {
		return nil, nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	return results, pager, nil
}

func Get(id string) (*models.SowingRecordDetail, *rich_err.RichError) {
	base := models.Orm.Model(&models.SowingRecord{}).
		Select("sowing_record.*, goadmin_users.name as manager_name,  orders.name as orders_name").
		Joins("LEFT JOIN goadmin_users ON sowing_record.manager_id = goadmin_users.id").
		Joins("LEFT JOIN orders ON sowing_record.order_id = orders.id")

	result, err := service.GetDetail[models.SowingRecordDetail](id, base)

	if err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	return result, nil
}

func Create(context *gin.Context) (*models.SowingRecordDetail, *rich_err.RichError) {
	req := new(models.SowingRecordCore)
	if err := context.ShouldBindJSON(req); err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	data := models.SowingRecord{SowingRecordCore: *req}
	result := models.Orm.Create(&data)
	if result.Error != nil {
		return nil, rich_err.NewError(rich_err.DatabaseFailed, result.Error)
	}
	err := orders.UpdateSowedQuantity(req.OrderID)
	if err != nil {
		return nil, rich_err.NewError(rich_err.DatabaseFailed, err)
	}
	// 更新订单成已播种
	if err := models.Orm.Model(&models.Orders{}).Where("id = ?", req.OrderID).Update("order_status", 3).Error; err != nil {
		return nil, rich_err.NewError(rich_err.DatabaseFailed, err)
	}

	return Get(strconv.Itoa(data.ID))
}

func Update(context *gin.Context) (*models.SowingRecordDetail, *rich_err.RichError) {
	req := new(models.SowingRecordDelta)
	if err := context.ShouldBindJSON(req); err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}

	data, err := service.Get[models.SowingRecord](context)
	if err != nil {
		return nil, rich_err.NewError(rich_err.RequestInvalid, err)
	}
	if req.OrderID != nil {
		data.OrderID = int(math.Round(req.OrderID.(float64)))
	}
	if req.ManagerID != nil {
		data.ManagerID = int(math.Round(req.ManagerID.(float64)))
	}

	if req.Quantity != nil {
		data.Quantity = int(math.Round(req.Quantity.(float64)))
	}
	if req.SowingDate != nil {
		data.SowingDate = utils.ToDateV2(req.SowingDate.(string))
	}
	if req.Remark != nil {
		data.Remark = req.Remark.(string)
	}
	result := models.Orm.Save(data)
	if result.Error != nil {
		return nil, rich_err.NewError(rich_err.DatabaseFailed, result.Error)
	}
	err = orders.UpdateSowedQuantity(data.OrderID)
	if err != nil {
		return nil, rich_err.NewError(rich_err.DatabaseFailed, err)
	}
	return Get(strconv.Itoa(data.ID))
}

func Delete(context *gin.Context) *rich_err.RichError {
	data, err := service.Get[models.SowingRecord](context)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil
		}
		return rich_err.NewError(rich_err.RequestInvalid, err)
	}
	result := models.Orm.Delete(data)
	if result.Error != nil {
		return rich_err.NewError(rich_err.DatabaseFailed, result.Error)
	}
	err = orders.UpdateSowedQuantity(data.OrderID)
	if err != nil {
		return rich_err.NewError(rich_err.DatabaseFailed, err)
	}
	return nil
}
